package com.situ.allaia.api;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.filter.AfterFilter;
import com.alibaba.fastjson2.filter.Filter;
import com.alibaba.fastjson2.filter.PropertyFilter;
import com.situ.allaia.model.Category;
import com.situ.allaia.service.CategoryService;
import com.situ.allaia.util.TreeUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

//自带ResponseBody响应json
@RestController
@RequestMapping(value = "/api/categories",produces = "application/json;charset=utf-8")
public class CategoryApi {
    private final CategoryService categoryService;

    public CategoryApi(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping
    public String findAll(){
        List<Category> categories = categoryService.findAll();
        //使用工具类生成树形结构
        TreeUtils.TreeNode<Category,Integer> root = TreeUtils.buildTreeByParentId(categories,Category::getParentId);
        //自动序列化,做一些自定义操作,序列化时遇到parent忽略
        PropertyFilter filter1 = new PropertyFilter() {
            @Override
            public boolean apply(Object object, String name, Object value) {
                if (object instanceof TreeUtils.TreeNode<?,?>){
                    if ("parent".equals(name)||"data".equals(name)){
                        return false;
                    }
                }
                return true;
            }
        };
        //后置过滤器
        AfterFilter filter2 = new AfterFilter() {
            @Override
            public void writeAfter(Object object) {
                if (object instanceof TreeUtils.TreeNode<?,?> node){
                    Category cat = (Category) node.getData();
                    if (cat!=null){
                        //自定义要序列化的属性
                        writeKeyValue("name",cat.getName());
                        writeKeyValue("parentId",cat.getParentId());
                        writeKeyValue("seq",cat.getSeq());
                        writeKeyValue("iconDesc",cat.getIconDesc());
                        writeKeyValue("gmtCreate",cat.getGmtCreate());
                        writeKeyValue("gmtUpdate",cat.getGmtUpdate());
                        writeKeyValue("description",cat.getDescription());
                        if (node.getParent()!=null){
                            Category pc = ((Category) (node.getParent().getData()));
                            if (pc!=null){
                                writeKeyValue("parentName",pc.getName());
                            }
                        }
                    }
                }
            }
        };
        return JSON.toJSONString(Map.of("code",0,"data",root.getChildren()),new Filter[]{filter1,filter2});
    }
    @GetMapping("/tree")
    public String findTreeSelect(){
        List<Category> categories = categoryService.findAll();
        //使用工具类生成树形结构
        TreeUtils.TreeNode<Category,Integer> root = TreeUtils.buildTreeByParentId(categories,Category::getParentId);
        //自动序列化,做一些自定义操作,序列化时遇到parent忽略
        PropertyFilter filter1 = new PropertyFilter() {
            @Override
            public boolean apply(Object object, String name, Object value) {
                if (object instanceof TreeUtils.TreeNode<?,?>){
                    if ("parent".equals(name)||"data".equals(name)){
                        return false;
                    }
                }
                return true;
            }
        };
        //后置过滤器
        AfterFilter filter2 = new AfterFilter() {
            @Override
            public void writeAfter(Object object) {
                if (object instanceof TreeUtils.TreeNode<?,?> node){
                    Category cat = (Category) node.getData();
                    if (cat!=null){
                        //自定义要序列化的属性
                        writeKeyValue("name",cat.getName());

                    }
                }
            }
        };
        return JSON.toJSONString(root.getChildren(),new Filter[]{filter1,filter2});
    }
    @PostMapping
    public Map<String,Object> save(Category category){
        //todo:参数校验
        boolean success = categoryService.save(category);
        return Map.of("success",success,"data",category);
    }
    @PutMapping
    public Map<String,Object> edit(Category category){
        //todo:参数校验
        boolean success = categoryService.update(category);
        return Map.of("success",success,"data",category);
    }
    @DeleteMapping
    public Map<String,Object> deleteByIds(Integer[] ids){
        int rows = categoryService.deleteByIds(Set.of(ids));

        return Map.of("success",rows>0,"rows",rows);
    }
}
